package com.ruoze.bdp.controller;

import com.ruoze.bdp.commons.ResponseDto;
import com.ruoze.bdp.entity.DataSourceEntity;
import com.ruoze.bdp.entity.DataSourceVo;
import com.ruoze.bdp.entity.datasource.DataSourceClusterVo;
import com.ruoze.bdp.entity.datasource.DataSourceRequest;
import com.ruoze.bdp.enums.ExceptionEnum;
import com.ruoze.bdp.service.DataSourceService;
import com.ruoze.bdp.utils.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/dataSource")
@RestController
public class DataSourceController {

    @Autowired
    private DataSourceService dataSourceService;

    @GetMapping("/list")
    public ResponseDto<Page<DataSourceClusterVo>> queryPage(@RequestParam(required = false) Long clusterId,
                                                            @RequestParam(required = false) String name,
                                                            @RequestParam(required = false) String type,
                                                            @RequestParam(defaultValue = "1") int pageNum,
                                                            @RequestParam(defaultValue = "10") int pageSize) {
        DataSourceRequest dataSourceRequest = new DataSourceRequest(clusterId, type, name, pageNum, pageSize);
        Page<DataSourceClusterVo> pageDto = dataSourceService.queryPage(dataSourceRequest);
        return ResponseUtil.successData(pageDto);
    }

    @GetMapping("/names")
    public ResponseDto<List<String>> queryNamesByType(@RequestParam String type) {
        List<String> names = dataSourceService.queryNamesByType(type);
        return ResponseUtil.successData(names);
    }

    @PostMapping("/add")
    public ResponseDto createNewDataSource(@RequestBody DataSourceVo newDataSource) {
        dataSourceService.createNewDataSource(newDataSource);
        return ResponseUtil.successData(ExceptionEnum.ADD_SUCCESS.getCode(),ExceptionEnum.ADD_SUCCESS.getMsg());
    }

    @PutMapping("/edit/{id}")
    public ResponseDto replaceNewDataSource(@RequestBody DataSourceVo newDataSource, @PathVariable long id) {
        dataSourceService.replaceNewDataSource(newDataSource, id);
        return ResponseUtil.successData(ExceptionEnum.EDIT_SUCCESS.getCode(),ExceptionEnum.EDIT_SUCCESS.getMsg());
    }

    @DeleteMapping("/delete/{id}")
    public ResponseDto deleteSingleDataSource(@PathVariable long id) {
        dataSourceService.logicalDelDataSource(id);
        return ResponseUtil.successData(ExceptionEnum.DELETE_SUCCESS.getCode(),ExceptionEnum.DELETE_SUCCESS.getMsg());
    }

    @DeleteMapping("/delete/batch")
    public ResponseDto deleteBatchDataSource(@RequestParam String ids) {
        List<Long> idList = new ArrayList<>();
        for (String id : ids.split(",")) {
            idList.add(Long.parseLong(id));
        }
        dataSourceService.logicalBatchDelDataSource(idList);
        return ResponseUtil.successData(ExceptionEnum.DELETE_SUCCESS.getCode(),ExceptionEnum.DELETE_SUCCESS.getMsg());
    }

    @PostMapping("/connect/test")
    public ResponseDto connectTest(@RequestParam String type, @RequestParam String connectInfo) {
        return dataSourceService.connectTest(type, connectInfo) ?
                ResponseUtil.successData(type+ "数据源连接成功") : ResponseUtil.fail(type+ "数据源连接失败");
    }

}
